// pages/customer/customer.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sticky: true,
    searchWord: '',
    stickyPx: 76,
    customers: {},
    indexList: [],
    kindObj: [],
    oldList: []
  },


  userHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    if (flag == 4) {
      wx.navigateTo({
        url: '/customerPackage/pages/addCustomer/addCustomer',
      })
    } else {
      var index = e.currentTarget.dataset.index,
        eIndex = e.currentTarget.dataset.eindex,
        list = this.data.kindObj,
        tel = list[index].child[eIndex].customerTel,
        cus = list[index].child[eIndex];
      if (flag == '1') {
        wx.makePhoneCall({
          phoneNumber: tel,
        })
      } else if (flag == 3) {
        wx.navigateTo({
          url: '/customerPackage/pages/customerDetail/customerDetail?customer=' + JSON.stringify(cus),
        })
      } else if (flag == 2) {
        wx.navigateTo({
          url: '/customerPackage/pages/visit/visit?customer=' + JSON.stringify(cus),
        })
      }
    }

  },

  selectUser(e) {
    var index = e.currentTarget.dataset.index,
      eIndex = e.currentTarget.dataset.eindex,
      kindObj = this.data.kindObj;
    kindObj.forEach((ele, eeIndex) => {
      ele.child.forEach((item, iIndex) => {
        if (eeIndex == index && iIndex == eIndex) {
          item.isSelect = !item.isSelect
        } else {
          item.isSelect = false
        }
      })
    })
    console.log(kindObj);
    this.setData({
      kindObj
    })
  },
  inputhandle(e) {
    console.log(e);
    var val = e.detail.value
    this.setData({
      searchWord: val
    })
    if (val == '') {
      this.dataHandle(this.data.oldList)
    } else {
      var list = this.data.oldList.filter(item => {
        return item.customerShopName.includes(val) || item.customerName.includes(val)
      })

      console.log(list);
      this.dataHandle(list)
    }
  },

  getList() {
    var url = app.globalData.url + "customer/lisUserCustomer",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var result = res.data
          wx.hideLoading()

          wx.stopPullDownRefresh()
          console.log(result);

          that.dataHandle(result)

          that.setData({
            oldList: result
          })
        }
      },
      err => {
        console.log(err);
      })
  },

  dataHandle(result) {
    var arr = [],
      map = {},
      indexList = [];
    for (var i in result) {
      result[i].isSelect = false
      result[i].flag = false;
      if (map.hasOwnProperty(result[i].customerFirstWord)) {
        map[result[i].customerFirstWord].push(result[i]);
      } else {
        indexList.push(result[i].customerFirstWord);
        map[result[i].customerFirstWord] = [result[i]];
      }
    }
    indexList.forEach(ele => {
      arr.push({
        word: ele,
        child: []
      })
    })
    arr.forEach(ele => {
      for (var i in map) {
        if (i == ele.word) {
          ele.child = map[i]
        }
      }
    })
    this.setData({
      kindObj: arr
    })
    console.log(this.data.kindObj);

  },

  // 拨打电话
  callHandle(e) {

    var index = e.currentTarget.dataset.index,
      flag = e.currentTarget.dataset.flag,
      eIndex = e.currentTarget.dataset.eindex,
      kindObj = this.data.kindObj,
      tel = kindObj[index].child[eIndex].customerTel;


    if (flag == '1') {
      wx.makePhoneCall({
        phoneNumber: tel,
      })
    } else if (flag == 2) {
      // 去拜访
    } else if (flag == 3) {

      // 查看详情
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})